package main

import(
	"runtime"
	"github.com/go-gl/gl/v2.1/gl"
	"github.com/go-gl/glfw/v3.2/glfw"
	mgl "github.com/go-gl/mathgl/mgl32"
	c"gogl/config"
	"log"
	"gogl/camera"
)
var ca *camera.Camera
func init() {
	// GLFW event handling must run on the main OS thread
	runtime.LockOSThread()
}

func main(){
	window:= initGlfw()
	defer glfw.Terminate()
	initOpenGL()
	//list.SetNet()
	ca = camera.NewCamera(mgl.Vec3{0,0,5},mgl.Vec3{0,1,0},camera.YAW,camera.PITCH)
	for !window.ShouldClose(){
		//t := time.Now()
		draw(window)
		//time.Sleep(time.Second/time.Duration(fps) - time.Since(t))
	}
	destoryAll()
}

func initGlfw() *glfw.Window{
	if err:= glfw.Init();err != nil{
		panic(err)
	}
	glfw.WindowHint(glfw.Resizable,glfw.False)
	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)
	//glfw.WindowHint(glfw.OpenGLProfile,glfw.OpenGLCoreProfile)

	window,err := glfw.CreateWindow(c.Windows_width,c.Windows_height,"love music",nil,nil)
	if err != nil{
		panic(err)
	}
	window.MakeContextCurrent()
	window.SetInputMode(glfw.CursorMode,glfw.CursorDisabled)
	window.SetKeyCallback(key_callback)
	window.SetCursorPosCallback(mouse_callback)
	window.SetScrollCallback(scroll_callback)
	return window
}

func initOpenGL(){
	if err := gl.Init(); err != nil {
		panic(err)
	}
	version := gl.GoStr(gl.GetString(gl.VERSION))
	log.Println("OpenGL version", version)
}

func draw(window *glfw.Window){
	gl.Clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT)
	callAllDraw()
	glfw.PollEvents()
	window.SwapBuffers()
}

func callAllDraw(){
	DrawLight()
	DrawCube()
}

func destoryAll(){
	DestroyLight()
	DestroyCube()
}